package org.millioncall.yueyoga.admin.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.common.SpringApplicationContextHolder;
import org.millioncall.yueyoga.admin.dao.AuthorityDAO;
import org.millioncall.yueyoga.admin.dao.ResourceDAO;
import org.millioncall.yueyoga.admin.model.Authority;
import org.millioncall.yueyoga.admin.model.Resource;
import org.millioncall.yueyoga.admin.service.AuthorityService;
import org.millioncall.yueyoga.common.security.ZhuwSecurityMetadataSource;
import org.springframework.stereotype.Service;

@Service
public class AuthorityServiceImpl implements AuthorityService {
	private AuthorityDAO authorityDAO;
	private ResourceDAO resourceDAO;
	
	public AuthorityDAO getAuthorityDAO() {
		return authorityDAO;
	}

	public void setAuthorityDAO(AuthorityDAO authorityDAO) {
		this.authorityDAO = authorityDAO;
	}

	public ResourceDAO getResourceDAO() {
		return resourceDAO;
	}

	public void setResourceDAO(ResourceDAO resourceDAO) {
		this.resourceDAO = resourceDAO;
	}

	@Override
	public void save(Authority authority) {
		this.authorityDAO.save(authority);
	}

	@Override
	public void delete(Authority authority) {
		this.authorityDAO.delete(authority);
	}

	@Override
	public Authority findById(int id) {
		return this.authorityDAO.findById(id);
	}

	@Override
	public List<Authority> findByProperty(String propertyName, Object value) {
		return this.authorityDAO.findByProperty(propertyName, value);
	}

	@Override
	public List<Authority> findByName(String name) {
		return this.authorityDAO.findByName(name);
	}

	@Override
	public List<Authority> findByEnabled(Boolean enabled) {
		return this.authorityDAO.findByEnabled(enabled);
	}

	@Override
	public List<Authority> findAll() {
		return this.authorityDAO.findAll();
	}

	@Override
	public List<Authority> findByEnabledWithResource(Boolean enabled) {
		List<Authority> authorities = this.authorityDAO.findByEnabled(enabled);
		if (authorities != null && !authorities.isEmpty()) {
			for (Authority authority : authorities) {
				List<Resource> resources = resourceDAO
						.findResourceByAuthorityId(authority.getId());
				authority.setResources(resources);
			}
		}
		return authorities;
	}

	@Override
	public boolean deleteAuthorities(String ids) {
		ZhuwSecurityMetadataSource zsms  =   SpringApplicationContextHolder.getBean("zhuwSecurityMetadataSource");
		zsms.loadResourceDefine();
		List<Integer> authorityIds = new ArrayList<Integer>();
		if (StringUtils.isNotBlank(ids)) {
			String[] aIds = ids.split(",");
			if (aIds != null && aIds.length > 0) {
				for (String id : aIds) {
					authorityIds.add(Integer.valueOf(id));
				}
				return this.authorityDAO.deleteAuthorities(authorityIds) > 0;
			}
		}
		return false;
	}

	@Override
	public boolean saveAuthority(Authority authority) {
		ZhuwSecurityMetadataSource zsms  =   SpringApplicationContextHolder.getBean("zhuwSecurityMetadataSource");
		zsms.loadResourceDefine();
		return this.authorityDAO.saveAuthority(authority) > 0;
	}

	@Override
	public PaginatedList<Authority> findAllAuthorities(int start, int limit,
			Map<String, String> map) {
		return this.authorityDAO.findAllAuthorities(start, limit, map);
	}

	@Override
	public boolean grantAuthorityResources(String auId, String rsId) {
		ZhuwSecurityMetadataSource zsms  =   SpringApplicationContextHolder.getBean("zhuwSecurityMetadataSource");
		zsms.loadResourceDefine();
		if (StringUtils.isNotBlank(auId) && StringUtils.isNotBlank(rsId)) {
			int count = 0;
			Map<String, String> map = new HashMap<String, String>();
			String[] ids = rsId.split(",");
			count = authorityDAO.deleteAuthorityResource(Integer.valueOf(auId));
			map.put("auId", auId);
			map.put("enabled", "1");
			if (ids != null && ids.length > 0) {
				for (String resId : ids) {
					if (StringUtils.isNotBlank(resId)) {
						map.put("rsId", resId);
						count += authorityDAO.saveAuthoritySource(map);
						map.remove("rsId");
					}
				}
			}
			return count > 0;
		}
		return false;
	}

	@Override
	public Authority findAuthorityByName(String name) {
		return this.authorityDAO.findAuthorityByName(name);
	}
}
